package top.stmo.springMVC.controller;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.stmo.springMVC.pojo.User;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *       请求获取参数
 */
@Controller
@RequestMapping("/user")
public class UserController {

    //普通参数  同名
    @ResponseBody
    @RequestMapping("/save1")
    public String save1(String name, int age) {
        System.out.println("user save");
        System.out.println("name：" + name + "   " + "age：" + age);
        return "user save";
    }

    //普通参数  不同名
    @ResponseBody
    @RequestMapping("/save2")
    public String save2(@RequestParam("name") String username, int age) {
        System.out.println("user save");
        System.out.println("name：" + username + "   " + "age：" + age);
        return "user save";
    }

    //pojo参数
    @ResponseBody
    @RequestMapping("/save3")
    public String save3(User user) {
        System.out.println("user save");
        System.out.println("User：" + user);
        return "user save";
    }

    //pojo参数    pojo参数中包含pojo参数
    @ResponseBody
    @RequestMapping("/save4")
    public String save4(User user) {
        System.out.println("user save");
        System.out.println("User：" + user);
        return "user save";
    }

    //数组参数
    @ResponseBody
    @RequestMapping("/save5")
    public String save5(String[] like) {
        System.out.println("user save");
        System.out.println(Arrays.toString(like));
        return "user save";
    }

    //集合参数
    @ResponseBody
    @RequestMapping("/save6")
    public String save6(@RequestParam List<String> like) {
        System.out.println("user save");
        System.out.println(like);
        return "user save";
    }

    //普通Json类型参数
    @ResponseBody
    @RequestMapping("/save7")
    public String save7(@RequestBody List<String> like) {
        System.out.println("user save");
        System.out.println(like);
        return "user save";
    }

    //pojo Json类型参数
    @ResponseBody
    @RequestMapping("/save8")
    public String save8(@RequestBody User user) {
        System.out.println("user save");
        System.out.println(user);
        return "user save";
    }

    //集合User Json类型参数
    @ResponseBody
    @RequestMapping("/save9")
    public String save9(@RequestBody List<User> users) {
        System.out.println("user save");
        System.out.println(users);
        return "user save";
    }

    //日期类型参数
    @ResponseBody
    @RequestMapping("/save")
    public String save10(Date date,
           @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2) {

        System.out.println("user save");
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);
        return "user save";
    }
}
